<template>
  <div class="spaceDetailPage">
    <a-flex gap="middle">
      <h2>
        {{ space.spaceName }}
        <span style="font-size: 14px; color: #999">({{ SPACE_TYPE_MAP[space.spaceType] }})</span>
      </h2>
      <a-space size="middle">
        <a-tooltip
          :title="`占用空间: ${formatSize(space.totalSize)} / ${formatSize(space.maxSize)}`"
        >
          <a-progress
            type="circle"
            :percent="parseFloat(((space.totalSize * 100) / space.maxSize).toFixed(1))"
            :size="42"
          />
        </a-tooltip>
        <a-button type="primary" :href="`/add_pic?spaceId=${space.id}`" target="_blank">
          添加图片
        </a-button>
        <a-button type="dashed" :href="`/spaceUserManage/${id}`" target="_blank">
          成员管理
        </a-button>
        <a-button
          :icon="h(BarChartOutlined)"
          :href="`/space_analyze?spaceId=${id}`"
          target="_blank"
        >
          空间分析
        </a-button>
        <a-button type="primary" danger :icon="h(EditOutlined)" @click="openBatchEditModal">
          批量编辑图片
        </a-button>
      </a-space>
    </a-flex>
    <div style="margin-bottom: 16px"></div>
    <!--搜索框-->
    <a-form layout="inline" :model="searchParam" @finish="doSearch">
      <a-form-item label="图片名称">
        <a-input v-model:value="searchParam.name" placeholder="请输入图片名称" allowClear />
      </a-form-item>
      <a-form-item label="分类">
        <a-select
          style="width: 120px"
          v-model:value="searchParam.category"
          placeholder="选择标签"
          :options="categoryOptions"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签">
        <a-select
          style="width: 120px"
          v-model:value="searchParam.tag"
          placeholder="请选择标签"
          :options="tagOptions"
          allowClear
        />
      </a-form-item>
      <a-form-item label="图片格式">
        <a-select
          ref="select"
          v-model:value="searchParam.picFormat"
          placeholder="请选择图片格式"
          style="width: 120px"
          allowClear
        >
          <a-select-option value="PNG">PNG</a-select-option>
          <a-select-option value="JPEG">JPEG</a-select-option>
          <a-select-option value="GIF">GIF</a-select-option>
          <a-select-option value="WEBP">WEBP</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <!--图片列表-->
    <picture-list
      :dataList="dataList"
      :loading="loading"
      :show-action="true"
      :on-reload="queryPicturePage"
    />
    <!--分页器-->
    <a-pagination
      v-model:current="searchParam.pageNum"
      v-model:page-size="searchParam.pageSize"
      :total="total"
      :show-total="(total) => `共 ${total} 条`"
      @change="onPageChange"
      :page-size-options="pageSizeOptions"
      show-size-changer
      style="text-align: right"
    >
      <template #buildOptionText="props">
        <span>{{ props.value }}条/页</span>
      </template>
    </a-pagination>
    <BatchEditPictureModal
      ref="batchEditPictureModalRef"
      :picture-list="dataList"
      :spaceId="id"
      :on-success="() => queryPicturePage()"
    />
  </div>
</template>
<script setup lang="ts">
import { h, onMounted, reactive, ref, watchEffect } from 'vue'
import { getSpaceVoByIdUsingGet } from '@/api/spaceController.ts'
import PictureList from '@/components/PictureList.vue'
import { getTagCategoryUsingGet, listPictureVoUsingGet } from '@/api/pictureController.ts'
import { formatSize } from '@/util'
import BatchEditPictureModal from '@/components/BatchEditPictureModal.vue'
import { BarChartOutlined, EditOutlined } from '@ant-design/icons-vue'
import { SPACE_TYPE_MAP } from '../../constants/space.ts'

interface Props {
  id: string
}

const props = defineProps<Props>()
const space = ref<API.SpaceVO>({})
const getSpaceVO = async () => {
  const res = await getSpaceVoByIdUsingGet({
    id: props.id,
  })
  space.value = (res?.data || {}) as API.SpaceVO
}
const dataList = ref<API.PictureVO[]>([])
const total = ref(0)
const loading = ref(false)
const pageSizeOptions = ref<string[]>(['5', '10', '20'])
// 搜索条件
const searchParam = reactive<API.listPictureVOUsingGETParams>({
  pageNum: 1,
  pageSize: 10,
  name: '',
  introduction: '',
  category: undefined,
  tag: undefined,
  picFormat: undefined,
})
// 获取图片数据
const queryPicturePage = async () => {
  loading.value = true
  const params = {
    spaceId: props.id,
    ...searchParam,
  }
  const res = await listPictureVoUsingGet(params)
  dataList.value = res.data.records ?? []
  total.value = res.data.total ?? 0
  loading.value = false
}
// 页码/pageSize改变时触发的方法
const onPageChange = (current: number, pageSize: number) => {
  searchParam.pageNum = current
  searchParam.pageSize = pageSize
  queryPicturePage()
}
const doSearch = () => {
  // 重置页码
  searchParam.pageNum = 1
  queryPicturePage()
}
const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])
const getTagCategoryOptions = async () => {
  const res = await getTagCategoryUsingGet()
  categoryOptions.value = (res?.data.categoryList || []).map((data: string) => {
    return {
      value: data,
      label: data,
    }
  })
  tagOptions.value = (res?.data.tagList || []).map((data: string) => {
    return {
      value: data,
      label: data,
    }
  })
}
const batchEditPictureModalRef = ref()
const openBatchEditModal = () => {
  if (batchEditPictureModalRef.value) {
    batchEditPictureModalRef.value.openModal()
  }
}
onMounted(() => {
  getSpaceVO()
  queryPicturePage()
  getTagCategoryOptions()
})
watchEffect(() => {
  getSpaceVO()
  queryPicturePage()
})
</script>

<style scoped></style>
